<?php namespace Org\JiwaNala\Theme;

use Input;

class ThemeDriver{
	var $rootPath;
	var $domainPaths;
	
	public function __construct($configs){
		$this->rootPath = $configs['rootPath'];
		$this->domainPaths = $configs['domainView'];
	}
	
	protected function resolveDomainViewDir($base){
		foreach($this->domainPaths as $viewPath=>$domainPatterns){
			foreach($domainPatterns as $pattern){
				if (str_is($pattern, $base)){
					return $viewPath;
				}
			}
		}
		
		return NULL;
	}
	
	public function rootPath(){
		return $this->rootPath();
	}
	
	public function domainPath(){
		$HTTP_HOST = Input::server('HTTP_HOST');
		return $this->resolveDomainViewDir($HTTP_HOST);
	}
	
	public function view($file, $domain=NULL){
		if (!$domain) {
			$domainPath = $this->domainPath();
		}
		else {
			$domainPath = $domain;
		}
		return $this->rootPath .($domainPath? '.'.$domainPath : ''). '.'.$file;
	}
	
	public function asset($file, $domain=NULL){
		if (!$domain) {
			$domainPath = $this->domainPath();
		}
		else {
			$domainPath = $domain;
		}
		return $this->rootPath .($domainPath? '/'.$domainPath : ''). '/'.$file;
	}
	
	public function style($file, $domain=null){
		return $this->asset($file, $domain);
	}
	
	public function script($file, $domain=null){
		return $this->asset($file, $domain);
	}
}